function DataMgr(){//docId,pageIndex,annotUrl
//	this.docID = docId;
//	this.pageIndex = pageIndex;
//	this.annotUrl = annotUrl;
	this.url = null;
	zip.workerScriptsPath = "./Editor1.0/zip/";
	this.zipFs = new zip.fs.FS();
	this.time1 = null;
	this.time2 = null;
	var _self = this;
	
	this.onerror = function(message) {
		console.error(message);
	};

	this.zipImportedZip = function (callback) {
		var directory = this.zipFs.root.addDirectory("import");
		directory.importHttpContent(this.url, false, function() {
			_self.zipFs.exportBlob(callback);
		}, onerror);
	};

	this.unzipBlob = function(blob, callback) {
		_self.zipFs.importBlob(blob, function() {
			var directory = _self.zipFs.root.getChildByName("import");
			var firstEntry = directory.children[0];
			firstEntry.getBlob("text/html",function(blob){
				var reader = new FileReader();
			    reader.onload = function (evt) {
			        var buffer = reader.result;
			        _self.time2 = new Date().getTime();
			        console.log(_self.time2 + "- " + _self.time1 + "=" + (_self.time2 - _self.time1));
			       callback(buffer);
			    };
			    try {
			        reader.readAsArrayBuffer(blob);
			    }
			    catch (e) {
			        alert(e);
			    }
			});
		}, onerror);
	};
	
	this.DownData = function(callback){
		_self.time1 = new Date().getTime();
		_self.zipImportedZip(function(zippedBlob) {
			_self.unzipBlob(zippedBlob, callback);
		});
	};
}

DataMgr.prototype.DownloadAnnotData = function(callback,_self){
	var xhrt = new XMLHttpRequest();
	xhrt.onreadystatechange = function () {
		if (xhrt.readyState == xhrt.DONE) {
			var status = "failed";
			if (xhrt.response) {
					if(xhrt.status == 200){
						readFileToArrayBuffer(xhrt.response,callback,_self);
						status = "success";
					}
			}
			if(status == "failed"){
				if(_self.callback != null){_self.callback(status);}
			}
		}
	};
	var url = this.annotUrl + "?fileid=" + this.docID + "&page=" + this.pageIndex;
	xhrt.open("POST", url, true);
	xhrt.responseType = "blob";
	xhrt.send(); 
};
DataMgr.prototype.DownloadData = function(docId,pageIndex,callback, caller){
		if(docId == null)return;
		this.docID = docId;
		this.pageIndex = (pageIndex == undefined || pageIndex == null)?0:pageIndex;
		var tempDataMgr = this;
		$.post(foxit.common.path+'/getPdfDocAction.action',
			{
				PK_PDF_DOC : this.docID,
				PAGE_NUMBER: this.pageIndex
			}, function(data){
				tempDataMgr.CheckData(data);
			}, 'json');
		var pdPage = pdDoc.GetPageByIndex(pageIndex);
		if (null == pdPage) return;
		if (pdPage.pdPageObjects.bFinishLoadImgs || pdPage.pdPageObjects.imgObjCount === 0) {
			callback.call(caller);
		}
		else {
			pdPage.pdPageObjects.callback = callback;
			pdPage.pdPageObjects.caller = caller;	
		}
};

//DataMgr.prototype.GetDataMgr = function(){
//	return dataMgr;
//}

DataMgr.prototype.CheckData = function (data){
	if (data.root.error!=undefined&&data.root.error!=null) {
			return;
	} else {
		var obj = data.root;
		var docTitle=obj.DOC_TITLE;
		var pageCount=obj.PAGE_COUNT;
		var docJsonData=eval("("+obj.DOC_JSON_CODE+")"); 
		var docVersion=obj.DOC_VERSION;
		var pageJsonData=eval("("+obj.PAGE_JSON_CODE+")");
		var jsonData=eval("("+obj.JSON_CODE+")");
		var pageNuamber=obj.PAGE_NUMBER;
												
		
		if(this.pageIndex == 0){
			this.ParseToDoc(pageCount,docTitle,docVersion,docJsonData);//加载文档级json
		}
		this.ParseToPage(pageJsonData);
		this.ParseToContent(jsonData);
		
	}
};
DataMgr.prototype.ParseToDoc = function(pageCount,docTitle,docVersion,docJsonData){
	pdDoc.docId = this.docID;
	pdDoc.SetCountPages(pageCount);
	pdDoc.docTitle = docTitle;
	pdDoc.docVersion = docVersion;
	pdDoc.ParseJsonToDoc(docJsonData);
};
DataMgr.prototype.ParseToPage = function(pageJsonData){
	if(pdDoc == null || pdDoc == undefined)return;
	pdDoc.AddPageLevelData(this.pageIndex,pageJsonData);
};
DataMgr.prototype.ParseToContent = function(jsonData){
	if(pdDoc == null || pdDoc == undefined)return;
	var pdPage = pdDoc.GetPageByIndex(this.pageIndex);
	if(pdPage == null)return;
	pdPage.AddPageObjects(jsonData);
};
